package com.school.test.search.function.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.NotAction;
import com.school.test.search.constants.RequestConst;
import com.school.test.search.function.base.controller.BaseTeaAndStuController;
import com.school.test.search.function.interceptor.NotNullInterceptor;
import com.school.test.search.function.service.TeacherService;
import com.school.test.search.utils.MyRegUtil;

import java.util.regex.Pattern;

/**
 * IndexController
 * @author 马洁
 */
@Before(NotNullInterceptor.class)
public class TeacherController extends BaseTeaAndStuController {
	@Inject
	private TeacherService service;

	public void index() {
		if (RequestConst.POST.equals(this.getRequest().getMethod())) {
			String name = getTrim("name");
			Pattern regStr = Pattern.compile(".*" + name + ".*");

			renderJson(service.searchName(regStr));
		} else {
			toSearch();
		}
	}

	@NotAction
	private void getTest(String teacherName) {
		// 获取老师姓名，转义掉空格
		String regTeacherName = MyRegUtil.escapeBracket(teacherName);

		// 处理所有考试并设置列表信息
		setTestListInfo(service.getTestList(regTeacherName));
		set("teacher", teacherName);

		render("test.html");
	}

	public void getTestByName() {
		String teacherName = getTrim("name");
		getTest(teacherName);
	}

	/**
	 * 不推荐使用，
	 * 此处为了兼容旧版本
	 */
	@Deprecated
	public void getTestListByTeacherName() {
		String teacherName = getTrim("teacherName");

		System.out.println("有问题的跳转，老师为 = " + teacherName);
		getTest(teacherName);
	}
}



